##
##    This file is part of Metrix.
##
##    Metrix is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Metrix is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with Metrix.  If not, see <http://www.gnu.org/licenses/>.
##

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:default => '%d-%m-%Y')

# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_metrix_session_id'

	## Comentar para desactivar TODA la seguridad
	before_filter :check_authentication#, :check_authorization

	def check_authentication
	  unless session[:user]
	    session[:intended_action] = action_name
	    session[:intended_controller] = controller_name
			flash[:notice] = 'Para acceder a esta sección deber identificarse como usuario registrado.'
	    redirect_to :action => 'index', :controller => 'login'
			return false
	  end
	end

	def check_authorization
		user = User.find(session[:user])
		unless user.roles.detect{|role|
		  role.rights.detect{|right|
		    #right.action == action_name && 
				right.controller == self.class.controller_path
		    }
		  }
		  flash[:notice] = 'Permiso denegado. Consulte al administrador del sistema.'
			redirect_to :controller => 'login', :action => 'unauthorized'
			return false
		 end
  end

  #def rescue_action_in_public(exception)
  #case exception
	#	  when ActiveRecord::RecordNotFound, ActionController::UnknownAction
	#	    render(:file   => "#{RAILS_ROOT}/public/404.html",
	#	           :status => "404 Not Found")
	#	  else
	#	    render(:file   => "#{RAILS_ROOT}/public/500.html",
	#	           :status => "500 Error")
	#	    SystemNotifier.deliver_exception_notification(
	#	      self, request, exception)
	#	end
	#end
end
